﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Home.Haushalt
{
    /// <summary>
    /// Zusammenfassung für Recripter.
    /// </summary>
    public class HCryptography
    {
        /// <summary></summary>
        private enum EAction { Encrypt, Decrypt }

        /// <summary></summary>
        private TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

        /// <summary></summary>
        public bool PrivateKeySeted = false;

        /// <summary></summary>
        public string PrivateKey
        {
            set
            {
                System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(value, new byte[0]);
                des.Key = pdb.GetBytes(16);
                pdb = null;
                des.Mode = CipherMode.ECB;
                PrivateKeySeted = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public HCryptography()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="privateKey"></param>
        public HCryptography(string privateKey)
        {
            PrivateKey = privateKey;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public byte[] Encrypt(Stream stream)
        {
            byte[] bufferIn = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(bufferIn, 0, (int)stream.Length);
            byte[] bufferOut = Encrypt(bufferIn);

            return bufferOut;
        }


        /// <summary>
        ///  klar -> geheim
        /// </summary>
        /// <param name="_buff"></param>
        /// <returns></returns>
        public byte[] Encrypt(byte[] _buff)
        {
            return des.CreateEncryptor().TransformFinalBlock(_buff, 0, _buff.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public byte[] Decrypt(Stream stream)
        {
            byte[] bufferIn = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(bufferIn, 0, (int)stream.Length);
            byte[] bufferOut = Decrypt(bufferIn);

            return bufferOut;
        }

        /// <summary>
        /// geheim -> klar
        /// </summary>
        /// <param name="buff"></param>
        /// <returns></returns>
        public byte[] Decrypt(byte[] buff)
        {
            return des.CreateDecryptor().TransformFinalBlock(buff, 0, buff.Length);
        }

        /// <summary>
        /// klar -> geheim
        /// </summary>
        /// <param name="wert"></param>
        /// <returns></returns>
        public string Encrypt64(string wert)
        {
            return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(wert)));
        }

        /// <summary>
        /// geheim -> klar
        /// </summary>
        /// <param name="wert"></param>
        /// <returns></returns>
        public string Decrypt64(string wert)
        {
            return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(wert)));
        }

        /// <summary>
        /// Encrypts the attribute.
        /// </summary>
        /// <param name="attributeValue">The attribute value.</param>
        /// <returns></returns>
        public string DecryptAttribute(string attributeValue)
        {
            try
            {
                if (attributeValue.StartsWith("Crypted:(") & attributeValue.EndsWith(")"))
                {
                    attributeValue = attributeValue.Substring(9, attributeValue.Length - 10);
                    attributeValue = Decrypt64(attributeValue);
                }
            }
            catch //(Exception e)
            {
                attributeValue = "";
            }

            return attributeValue;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool FileEncrypt(string filename)
        {
            return Crypt(filename, EAction.Encrypt);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool FileDecrypt(string filename)
        {
            return Crypt(filename, EAction.Decrypt);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        private bool Crypt(string filename, EAction action)
        {

            try
            {
                byte[] daten = System.IO.File.ReadAllBytes(filename);

                byte[] daten2 = new byte[0];

                if (action == EAction.Encrypt)
                    daten2 = Encrypt(daten);
                else
                    daten2 = Decrypt(daten);


                FileStream fs = System.IO.File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                fs.SetLength(0);
                fs.Write(daten2, 0, daten2.Length);
                fs.Close();

            }
            catch
            {
                return false;
            }
            return true;
        }


    }
}
